﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace YH.Bluetooth
{
    public partial class Form3 : Form
    {
        // 加载所需的 DLL 文件
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr LoadLibrary(string lpFileName);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        private static extern bool FreeLibrary(IntPtr hModule);
        public Form3()
        {
            InitializeComponent();
            // 使用一个列表来保存已加载的库句柄，以便稍后可以释放它们
            //var loadedLibraries = new List<IntPtr>();

            //try
            //{
            //    foreach (var dll in new[] { "filter.dll" })
            //    {
            //        string fullPath = Path.Combine(_appPath, dll);
            //        IntPtr handle = LoadLibrary(fullPath);
            //        if (handle == IntPtr.Zero)
            //        {
            //            throw new Exception($"Failed to load library: {fullPath}, Error: {Marshal.GetLastWin32Error()}");
            //        }
            //        loadedLibraries.Add(handle);
            //    }
            //}
            //catch (Exception ex)
            //{
            //    // 如果加载失败，尝试释放已经加载的库
            //    foreach (var handle in loadedLibraries)
            //    {
            //        FreeLibrary(handle);
            //    }
            //    throw ex; // 重新抛出异常
            //}
        }
        // 确定应用程序的基础目录
        private static readonly string _appPath = Path.GetDirectoryName(typeof(FlexoDllUtil).Assembly.Location);
        [DllImport("filter.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void filter(float[] source, int sourceLen, float[] target, int b);

        // 你可以创建一个静态方法来更方便地使用这个功能
        public static float[] ApplyFilter(float[] input, int b)
        {
            float[] output = new float[input.Length];
            filter(input, input.Length, output, b);
            return output;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                float[] testData = new float[401];
                for (int i = 0; i < testData.Length; i++)
                {
                    testData[i] = i;
                }
                // 输出结果
                Console.WriteLine("滤波之前的数据:");
                foreach (float f in testData)
                {
                    Console.WriteLine(f);
                }
                float[] filteredData = ApplyFilter(testData, testData.Length);
                // 输出结果
                Console.WriteLine("Filtered Data:");
                foreach (float f in filteredData)
                {
                    Console.WriteLine(f);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}
